package com.dinian.zeus.controller;

import com.dinian.zeus.domain.Newspaper;
import com.dinian.zeus.domain.Periodical;
import com.dinian.zeus.domain.ResponseVO;
import com.dinian.zeus.util.Result;
import com.dinian.zeus.domain.SearchVO;
import com.dinian.zeus.service.NewspaperService;
import com.dinian.zeus.service.PeriodicalService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @Author dinian
 * @Date 2019-01-02
 */
@Slf4j
@RestController
public class SearchController {

    @Autowired
    private NewspaperService newspaperService;

    @Autowired
    private PeriodicalService periodicalService;
    /**
     * 多条件搜索
     * @param searchVO
     * @return
     */
    @RequestMapping("/search")
    public Result searchPeriodical(SearchVO searchVO){
        log.info(searchVO.toString());
        if ("报纸".equals(searchVO.getCategory())){
            Newspaper newspaper = new Newspaper();
            if ("征订号".equals(searchVO.getSearchField())){
                newspaper.setNewsCode(searchVO.getSearchInput());
            }else if("名称".equals(searchVO.getSearchField())){
                newspaper.setNewsName(searchVO.getSearchInput());
            }else if("排架号".equals(searchVO.getSearchField())){
                newspaper.setNewsIndex(searchVO.getSearchInput());
            }else {
                return Result.error("字段选择异常");
            }
            List<ResponseVO> newspapers = newspaperService.searchNewspaper(newspaper);
            return Result.ok(newspapers);
        }else if ("期刊".equals(searchVO.getCategory())){
            Periodical periodical = new Periodical();
            if ("征订号".equals(searchVO.getSearchField())){
                periodical.setPeriodicalCode(searchVO.getSearchInput());
            }else if("名称".equals(searchVO.getSearchField())){
                periodical.setPeriodicalName(searchVO.getSearchInput());
            }else if("排架号".equals(searchVO.getSearchField())){
                periodical.setPeriodicalIndex(searchVO.getSearchInput());
            }else {
                return Result.error("字段选择异常");
            }
            List<ResponseVO> periodicals = periodicalService.searchPeriodical(periodical);
            return Result.ok(periodicals);
        }
        return Result.error();
    }
}
